//
//  HMForgetPossWordViewController.m
//  获得
//
//  Created by mac on 17/1/7.
//  Copyright © 2017年 mac.gu. All rights reserved.
//

#import "HMNetWorkTool.h"
#import "HMPhoneNum.h"
#import "UIViewController+Extension.h"
#import "HMPhoneNumForgetViewController.h"
#import "UIColor+Addition.h"
#import <SMS_SDK/SMSSDK.h>
#import "TPKeyboardAvoidingScrollView.h"
@interface HMPhoneNumForgetViewController ()



{
    NSInteger  buttonIndex;
}
//用户名
@property (nonatomic, strong) UITextField *txtUserName;
//验证码
@property (nonatomic, strong) UITextField *txtCode;
//获取验证码
@property (nonatomic, strong) UIButton *btnCode;
//输入密码
@property (nonatomic, strong) UITextField *txtPassWord;

@property (nonatomic, strong) UIButton *btnLogin;
//定时器
@property (nonatomic,retain) NSTimer * timer;

@end

@implementation HMPhoneNumForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    buttonIndex = 60;
    //自定义返回按钮
    //    self.navigationItem.leftBarButtonItem = [UIBarButtonItemExtension leftBackButtonItem:@selector(backAction) andTarget:self];
    self.view = [[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [self setupUI];
    
}


- (void)setupUI{
    
    [self.view addSubview:self.txtUserName];
    [_txtUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.offset(260);
        make.top.offset(64);
        make.height.offset(40);
    }];
    
    [self.view addSubview:self.txtCode];
    [_txtCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_txtUserName);
        make.top.equalTo(_txtUserName.mas_bottom).offset(15);
        make.height.equalTo(_txtUserName);
        make.width.offset(150);
    }];
    
    [self.view addSubview:self.btnCode];
    
    [_btnCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_txtUserName);
        make.height.top.equalTo(_txtCode);
        make.width.offset(60);
        
    }];
    
    [self.view addSubview:self.txtPassWord];
    [_txtPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(_txtUserName);
        make.top.equalTo(_btnCode.mas_bottom).offset(15);
        
    }];
    
    [self.view addSubview:self.btnLogin];
    [_btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(_txtUserName);
        make.top.equalTo(_txtPassWord.mas_bottom).offset(30);
        make.height.offset(40);
    }];
}

//添加用户名
- (UITextField *)txtUserName{
    if (!_txtUserName) {
        _txtUserName = [[UITextField alloc] init];
        //是否纠错
        
        _txtUserName.autocorrectionType = UITextAutocorrectionTypeNo;
        _txtUserName.keyboardType = UIKeyboardTypeNumberPad;
        _txtUserName.font = [UIFont systemFontOfSize:15];
        _txtUserName.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithHex:0x999999]}];
        
        _txtUserName.textColor = [UIColor orangeColor];
        //边框颜色
        _txtUserName.layer.borderColor = [UIColor colorWithHex:0xcccccc].CGColor;
        //边框宽度
        _txtUserName.layer.borderWidth = 0.5;
        _txtUserName.layer.masksToBounds = YES;
        _txtUserName.layer.cornerRadius = 4;
        _txtUserName.backgroundColor = [UIColor colorWithHex:0xffffff];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
        _txtUserName.leftView = img;
        
        _txtUserName.leftViewMode = UITextFieldViewModeAlways;
    }
    return _txtUserName;
}

//验证码
- (UITextField *)txtCode{
    if (!_txtCode) {
        _txtCode = [[UITextField alloc] init];
        //是否纠错
        _txtCode.autocorrectionType = UITextAutocorrectionTypeNo;
        _txtCode.keyboardType = UIKeyboardTypeNumberPad;
        _txtCode.font = [UIFont systemFontOfSize:15];
        _txtCode.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithHex:0x999999]}];
        _txtCode.textColor = [UIColor orangeColor];
        _txtCode.layer.borderColor = [UIColor colorWithHex:0xcccccc].CGColor;
        _txtCode.layer.borderWidth = 0.5;
        _txtCode.layer.masksToBounds = YES;
        _txtCode.layer.cornerRadius = 4;
        _txtCode.backgroundColor = [UIColor colorWithHex:0xffffff];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
        _txtCode.leftView = img;
        
        _txtCode.leftViewMode = UITextFieldViewModeAlways;
    }
    return _txtCode;
}

// 获取验证码
- (UIButton *)btnCode{
    if (!_btnCode) {
        _btnCode = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnCode setTitleColor:[UIColor colorWithHex:0xffffff] forState:UIControlStateNormal];
        _btnCode.titleLabel.font = [UIFont systemFontOfSize:15];
        [_btnCode setTitle:@"获取" forState:UIControlStateNormal];
        _btnCode.layer.masksToBounds = YES;
        _btnCode.layer.cornerRadius = 5;
        _btnCode.backgroundColor = [UIColor orangeColor];
        
        [_btnCode addTarget:self action:@selector(btnChagenCodeAction) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return _btnCode;
}

//修改密码 获取验证码点击事件
- (void)btnChagenCodeAction{
    if ([HMPhoneNum isMobile:_txtUserName.text] != YES) {
        [self showHudAuto:@"请输入正确手机号" andDuration:@"1"];
    }else{
        if (buttonIndex == 60 || self.timer == nil) {
            [self creatTimer];
            
            [self showHudWaitingView:@"正在发送..."];
            WeakSelf(HMPhoneNumForgetViewController);
            
            [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:weakSelf.txtUserName.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
                [weakSelf removeMBProgressHudInManaual];
                if (!error) {
                    
                    [weakSelf showHudAuto:@"发送成功" andDuration:@"1"];
                    [weakSelf stopTimer];
                    
                }else{
                    [weakSelf showHudAuto:@"发送失败" andDuration:@"1"];
                }
                
            }];
            //            [[HMNetWorkTool sharedTool]requstCodeNum:weakSelf.txtUserName.text Finished:^(id responseObject) {
            //                [weakSelf showHudAuto:@"发送成功" andDuration:@"1"];
            //                [weakSelf stopTimer];
            //            } Failed:^(NSError *error) {
            //                [weakSelf showHudAuto:@"发送失败" andDuration:@"1"];
            //            }];
            
        }
    }
}
//移除定时器
- (void)stopTimer{
    [self.timer invalidate];
    
    self.timer = nil;
}

//输入密码
- (UITextField *)txtPassWord{
    if (!_txtPassWord) {
        _txtPassWord = [[UITextField alloc] init];
        //是否纠错
        _txtPassWord.autocorrectionType = UITextAutocorrectionTypeNo;
        _txtPassWord.font = [UIFont systemFontOfSize:15];
        _txtPassWord.secureTextEntry = YES;
        _txtPassWord.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithHex:0x999999]}];
        _txtPassWord.textColor = [UIColor orangeColor];
        _txtPassWord.layer.borderColor = [UIColor colorWithHex:0xcccccc].CGColor;
        _txtPassWord.layer.borderWidth = 0.5;
        _txtPassWord.layer.masksToBounds = YES;
        _txtPassWord.layer.cornerRadius = 4;
        _txtPassWord.backgroundColor = [UIColor colorWithHex:0xffffff];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
        _txtPassWord.leftView = img;
        
        _txtPassWord.leftViewMode = UITextFieldViewModeAlways;
    }
    return _txtPassWord;
}


//登录
- (UIButton *)btnLogin{
    if (!_btnLogin) {
        _btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        ;
        [_btnLogin setTitleColor:[UIColor colorWithHex:0xffffff] forState:UIControlStateNormal];
        _btnLogin.titleLabel.font = [UIFont systemFontOfSize:17];
        [_btnLogin setTitle:@"确认" forState:UIControlStateNormal];
        _btnLogin.layer.masksToBounds = YES;
        _btnLogin.layer.cornerRadius = 5;
        _btnLogin.backgroundColor = [UIColor orangeColor];
        
        [_btnLogin addTarget:self action:@selector(btnSaveAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _btnLogin;
}

- (void)btnSaveAction{
    if ([HMPhoneNum isMobile:self.txtUserName.text] != YES) {
        [self showHudAuto:@"请输入正确手机号" andDuration:@"1"];
    }else if ([self.txtCode.text isEqualToString:@""]) {
        [self showHudAuto:@"请输入验证码" andDuration:@"1"];
    }else if ([HMPhoneNum validatePassword:self.txtPassWord.text] != YES) {
        [self showHudAuto:@"密码长度不少于6位" andDuration:@"1"];
    }{
        [self getPasswdNetWork];
    }
}



- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatTimer
{
    _timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(appAction) userInfo:nil repeats:YES];
}
- (void)appAction
{
    [self.btnCode setTitle:[NSString stringWithFormat:@"%ld秒",(long)buttonIndex--] forState:UIControlStateNormal];
    if (buttonIndex<=0) {
        [_timer setFireDate:[NSDate distantFuture]];
        [self.btnCode setTitle:[NSString stringWithFormat:@"重新获取"] forState:UIControlStateNormal];
        buttonIndex=60;
    }
}

//忘记密码 调用
- (void)getPasswdNetWork{
    WeakSelf(HMPhoneNumForgetViewController);
    
    [[HMNetWorkTool sharedTool]requstCommitCode:self.txtCode.text andPhoneNum:_txtUserName.text Finished:^(id responseObject) {
        
        [weakSelf showHudAuto:@"验证成功" andDuration:@"1"];
    
    } Failed:^(NSError *error) {
        
    }];
    
    
    
}


- (NSString *)title{
    return @"忘记密码";
}

- (void)dealloc{
    if ([self.timer isValid]) {
        [self.timer invalidate];
        self.timer = nil;
    }
}



@end
